<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class ActivityController extends CommonController {
	
	function index(){
		$this->display('index');
	}
	
	/**
	 * [research_list 问卷调查列表]
	 * author  [lzm]
	 * time    2016年7月5日
 	 * @return [array] [description]
	 */
	public function research_list(){
		$research=M('research');
		$list=$research->select();
		$this->assign('list',$list);
		$this->display('index');
	}
	//添加问卷调查页面
	public function add_research_index(){
		//选择所有奖品列表
		$arr=M('award')->where('endtime>"'.time().'"')->select();
		//echo M()->_sql();exit('@@@');
		$this->assign('arr',$arr);
		$this->display('add_research');
	}
	//添加问卷调查内容
	public function add_research(){
		$research=M('research');
		//接收参数
		$state=I('state');
		$img=I('cover');
		if ($state==1) {
			$arr=$research->where('state=1')->find();
			//print_r($arr);exit;
			if (!empty($arr)) {
				//exit('123'); 
			   error('只能开启一个问卷调查',$_SERVER['HTTP_REFERER']);
			}
		}
		$aid=serialize(I('checkbox'));
		//print_r($aid);exit;
		$name=trim(I('title'));
		$description=trim(I('description'));
		$ctime=date('Y-m-d H:i:s',time());
		$mtime=date('Y-m-d H:i:s',time());
		$data=array(
			'aid'=>$aid,
			'name'=>$name,
			'img'=>$img,
			'description'=>$description,
			'ctime'=>$ctime,
			'mtime'=>$mtime,
			'state'=>$state,
			);
		//print_r($data);exit('@#$%');
		$res_add=$research->data($data)->add();
		if (false !== $res_add) {
			 $this->redirect('Activity/research_list');
		}else{
			  error('操作失败',$_SERVER['HTTP_REFERER']);
		}
		
	}
	//删除问卷调查
	public function del_research(){
		$id=I('id');
		$research=M('research');
		//删除调查表数据
		$res_del=$research->where('id='.$id)->delete();
		//删除问题表数据
		$result_del=M('question')->where('rid="'.$id.'"')->delete();
		//echo M()->_sql();exit;
		if (false !== $res_del && false !== $result_del) {
		    $this->redirect('Activity/research_list');
		}else{
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}
	}

	//修改内容显示
	public function update_index(){
		$research=M('research');
		$id=I('id');
		$list=$research->where('id="'.$id.'"')->find();
		//查出对应的奖品
		$aid=unserialize($list['aid']);
		$arr=M('award')->where('endtime>"'.time().'"')->select();
		$this->assign('v',$arr);
		$this->assign('arr',$aid);
		$this->assign('list',$list);
		$this->display('update_research');
	}
	//修改内容
	public function update_research(){
		$research=M('research');
		//接收参数
		$state=I('state');
		$img=I('cover');
		if ($state==1) {
			$arr=$research->where('state=1')->find();
			//print_r($arr);exit;
			if (!empty($arr)) {
				 error('只能开启一个问卷调查',$_SERVER['HTTP_REFERER']);
			}
		}
		$id=I('id');

		$aid=serialize(I("checkbox"));

		$name=trim(I('title'));
		$description=trim(I('description'));
		$mtime=date('Y-m-d H:i:s',time());
		$data=array(
			'name'=>$name,
			'description'=>$description,
			'img'=>$img,
			'mtime'=>$mtime,
			'state'=>$state,
			'aid'=>$aid,
			);
		//print_r($data);exit;
		$res_save=$research->where('id="'.$id.'"')->data($data)->save();
		if (false !== $res_save) {
			 $this->redirect('Activity/research_list');
		}else{
			 error('操作失败',$_SERVER['HTTP_REFERER']);
			 //$this->redirect('Activity/research_list');
		}
	}
	



	//问题应答首页
	public function question(){
		$id=I('id');
		//print_r($id);exit;
		$question=M('question');
		$list=$question->where('rid="'.$id.'"')->select();
		$this->assign('id',$id);
		$this->assign('list',$list);
		$this->display('question');
		//echo 'hello';
	}
	//添加问题首页
	public function add_question(){
		$id=I('id');
		//print_r($id);exit;
		$this->assign('id',$id);
		$this->display('add_question');
	}
	//添加问题到数据库
	public function question_add(){
		$rid=I('id');
		//print_r($rid);exit;
		$type=I('type');
		$question=trim(I('question'));
		$checkbox=serialize(I('name'));

		$data=array(
			'rid'=>$rid,
			'name'=>$question,
			'type'=>$type,
			'checkbox'=>$checkbox,
			'ctime'=>date("Y-m-d H:i:s",time()),
			'mtime'=>date("Y-m-d H:i:s",time()),
			);
		$res_add=M('question')->data($data)->add();
		if (false == $res_add){
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}else{
			$this->redirect('Activity/question',array('id' => $rid));

		}
	}


	//删除问题
	public function del_question(){
		$id=I('id');
		$res=M('question')->where('id="'.$id.'"')->find();
		$rid=$res['rid'];
		$res_del=M('question')->where('id="'.$id.'"')->delete();
		if (false !== $res_del) {
		    $this->redirect('Activity/question',array('id' => $rid));
		}else{
			exit('fail');
		    $this->redirect('Activity/question');
		}
	}


	//修改问题页面
	public function update_question_index(){
		$id=I('id');
		$list=M('question')->where('id="'.$id.'"')->find();
		$name=unserialize($list['checkbox']);
		foreach ($name as $value) {
			 $arr[]=$value;
		}
		$this->assign('name',$name);
		$this->assign('list',$list);
		$this->display('update_question');

	}
	//修改问题
	public function question_update(){
		$id=I('id');
		$rid=I('rid');
		$type=I('type');
		$question=trim(I('question'));
		$checkbox=serialize(I('name'));
		$data=array(
			'name'=>$question,
			'type'=>$type,
			'checkbox'=>$checkbox,
			'mtime'=>date("Y-m-d H:i:s",time()),
			);
		//print_r($data);exit;
		$res_save=M('question')->where('id="'.$id.'"')->data($data)->save();
		//echo M()->_sql();exit;
		if (false == $res_save){
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}else{
			$this->redirect('Activity/question',array('id' => $rid));
		}
	}
	
}